import { Notification } from "element-ui"
// NOTIF消息提示
const NOTIF: any = Notification;
export const $notify = (msg = "操作成功", type = "success", offset = 50) => {
  NOTIF.closeAll();
  NOTIF({
    type: type,
    customClass: type,
    offset,
    message: msg,
  });
};
export const isIE = () => {
  if (!!window.ActiveXObject || "ActiveXObject" in window) {
    return true;
  } else {
    return false;
  }
};
//时间格式化
export const formatDate = (timestamp: any, format = "yyyy-MM-dd hh:mm:ss") => {
  (Date.prototype as any).format = function (format: any) {
    const o: any = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      S: this.getMilliseconds(),
    };
    if (/(y+)/.test(format)) {
      format = format.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (const k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
      }
    }
    return format;
  };
  if (isIE() && timestamp - 0 + "" == "NaN") {
    return (new Date(Date.parse(timestamp.replace(/-/g, "/"))) as any).format(
      format
    );
  } else {
    return (new Date(timestamp) as any).format(format);
  }
};
// 深拷贝
export const deepClone = (data: any) => {
  return JSON.parse(JSON.stringify(data));
};
// 存储localStorage
export const setLocal = (key: string, value: any) => {
  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  localStorage.setItem(key, value);
};

// 获取localStorage
export const getLocal = (key: string) => {
  let value = localStorage.getItem(key);
  if (value && value.includes("{") && value.includes("}")) {
    value = JSON.parse(value);
  }
  return value || "";
};

// 删除localStorage
export const removeLocal = (key: string) => {
  localStorage.removeItem(key);
};

// 获取echarts
export const getChart = (chart: string) => {
  return new Promise(resolve => {
    const rollPolling = () => {
      if (chart) {
        resolve(chart);
      } else {
        setTimeout(() => {
          rollPolling();
        }, 10);
      }
    };
    rollPolling();
  });
};
// 获取echarts需要绑定的dom
export const getRef = (refName: any) => {
  return new Promise(resolve => {
    const rollPolling = () => {
      if (refName) {
        resolve(refName);
      } else {
        setTimeout(() => {
          rollPolling();
        }, 10);
      }
    };
    rollPolling();
  });
};
